package com.thejoyrun.retrofit.http.Interceptor;

import java.io.IOException;

import okhttp3.FormBody;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by kkmike999 on 2016/05/08.
 *
 * .Net服务器需要在参数中，传uid & sid
 */
public class TokenInterceptor implements Interceptor {

	@Override
	public Response intercept(Interceptor.Chain chain) throws IOException {
		Request original = chain.request();

		FormBody formBody = (FormBody) original.body();

		FormBody.Builder builder = new FormBody.Builder();

		for (int i = 0; i < formBody.size(); i++) {
			builder.add(formBody.name(i), formBody.value(i));
		}

		builder.add("uid", String.valueOf(1));
		builder.add("sid", "sid");

		//请求定制：添加请求头
		Request.Builder requestBuilder = original.newBuilder()
		                                         .method(original.method(), builder.build());

		Request request = requestBuilder.build();
		return chain.proceed(request);
	}
}
